package co.com.javeriana.aes.patrones.universalbank.mainframe.client;

import co.com.javeriana.aes.patrones.universalbank.dto.Account;
import co.com.javeriana.aes.patrones.universalbank.dto.WrapperList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MainFrameClient {

    private static BufferedReader in;
    private static PrintWriter out;
    private static final int port = 9898;
    private static final String serverAddress = "localhost";

    public MainFrameClient() {

//        try {
//
//            connectToServer();
//
//            //para traer cuentas
//            //out.println("1136879159");
//            //processAccounts(in.readLine());
//            //para el caso de descontar
//            out.println("U1136879159,200458835,17");
//            System.out.println(in.readLine());
//
//            //para finalizar la conexion con el server socket
//            out.println("");
//
//        } catch (IOException ex) {
//            Logger.getLogger(MainFrameClient.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }

    public static String processDisscount(String istring) {
        
        String answer = "";
        
        try {           
        
            connectToServer();
            //para el caso de descontar
            out.println(istring);
            //out.println("U1136879159,200458835,17");
            answer = in.readLine();
            System.out.println(answer);

            //para finalizar la conexion con el server socket
            out.println("");
            
           } catch (Exception e) {
               e.printStackTrace();
        }
        return answer;
    }
    
    public static WrapperList processAccounts(String istring) {

        ArrayList<Account> accounts = null;
        

        try {

            connectToServer();

            //para traer cuentas
            out.println(istring);

            StringTokenizer token = new StringTokenizer(in.readLine(), ",");

            accounts = new ArrayList<Account>();

            while (token.hasMoreElements()) {
                Account ac = new Account();
                ac.setUserId (Integer.valueOf( token.nextElement().toString()));
                ac.setAcountId(Integer.valueOf(token.nextElement().toString()));
                ac.setAccountBalance(Long.valueOf(token.nextElement().toString()));
                accounts.add(ac);
            }

            //para finalizar la conexion con el server socket
            out.println("");

        } catch (Exception e) {
            e.printStackTrace();
        }
        
        //Account[] myArray = accounts.toArray(new Account[accounts.size()]);
        
        return new WrapperList(accounts);
        
    }
    
    private static void connectToServer() throws IOException {

        // Make connection and initialize streams
        Socket socket = new Socket(serverAddress, port);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(socket.getOutputStream(), true);
    }

//    /**
//     * Runs the client application.
//     *
//     * @param args
//     * @throws java.lang.Exception
//     */
//    public static void main(String[] args) throws Exception {
//        //MainFrameClient client = new MainFrameClient();
//        List<Account> processAccounts = processAccounts("1136879159");
//                for (Account account : processAccounts) {
//                    System.out.println(account.getAcountId());
//        }
//    }
}
